<template>
  <div class="page-box">
    <!-- 筛选区域 -->
    <div v-show="flag" class="search-box">
      <!-- :inline="true" 表单在一行显示 -->
      <el-form size="small" :inline="true" class="demo-form-inline">
        <el-form-item label="标题">
          <el-input v-model="params.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="分类" prop="cateid">
          <el-select v-model="params.cateid" placeholder="请选择">
            <el-option v-for="item in catelist" :key="item.id" :label="item.catename" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker v-model="params.create_date" class="date" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="timestamp" />
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="serach">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 工具条区域 -->
    <div class="tabbar-box">
      <el-row type="flex" justify="space-between">
        <div>
          <el-button plain type="primary" icon="el-icon-plus" size="mini" @click="$router.push('/content_article/add')">新增</el-button>
          <el-button plain type="warning" icon="el-icon-download" size="mini" @click="exportExcel">导出</el-button>
        </div>
        <div>
          <el-tooltip class="item" effect="dark" content="关闭搜索区" placement="top">
            <el-button :icon="up" size="small" circle @click="hide" />
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="刷新" placement="top">
            <el-button icon="el-icon-refresh" size="small" circle @click="reload" />
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="筛选" placement="top-start">
            <el-popover placement="bottom" width="100" trigger="click">
              <el-checkbox-group v-model="checkList">
                <el-checkbox v-for="(item,index) in renderList" :key="index" class="fa fa fa-dedent" :label="item.name" />
              </el-checkbox-group>
              <el-button slot="reference" class="filter" icon="el-icon-menu" size="small" circle />
            </el-popover>
          </el-tooltip>
        </div>
      </el-row>
    </div>
    <!-- 表格区域 -->
    <div class="table-box">
      <el-table id="outTable" ref="myTable" :data="articlelist" style="width: 100%" border>
        <el-table-column v-if="checkList.includes('ID')" prop="id" label="ID" />
        <el-table-column v-if="checkList.includes('标题')" prop="title" label="标题" />
        <el-table-column v-if="checkList.includes('分类名')" prop="catename" label="分类名" />
        <el-table-column v-if="checkList.includes('图片')" prop="" label="图片">
          <template #default="{row}">
            <el-image style="width: 100px; height: 100px" :src="'http://124.223.14.236:8060/'+row.pic" :fit="'cover'" />
          </template>
        </el-table-column>
        <el-table-column v-if="checkList.includes('创建时间')" prop="" label="创建时间">
          <template #default="{row}">
            {{ new Date(row.create_date).toLocaleDateString() }}
          </template>
        </el-table-column>
        <el-table-column v-if="checkList.includes('是否推荐')" prop="" label="是否推荐">
          <template #default="{row}">
            <el-switch :active-value="1" :inactive-value="0" :value="row.ishot" />
          </template>
        </el-table-column>
        <el-table-column v-if="checkList.includes('是否置顶')" prop="" label="是否置顶">
          <template #default="{row}">
            <el-switch :active-value="1" :inactive-value="0" :value="row.istop" />
          </template>
        </el-table-column>
        <el-table-column v-if="checkList.includes('文章状态')" prop="" label="文章状态">
          <template #default="{row}">
            <div class="status">{{ row.status?'待审核':'已发布' }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="" label="操作">
          <template #default="{row}">
            <el-button size="mini" circle type="primary" icon="el-icon-edit" @click="$router.push(`/content_article/edit/${row.id}`)" />
            <el-button size="mini" type="danger" icon="el-icon-delete" circle @click="dodelete(row.id)" />
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页区域 -->
    <!-- <div class="page">
      <el-pagination
        v-if="isTableAlive"
        background
        :current-page="params.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="params.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div> -->
    <CutPage v-if="isTableAlive" :total="total" :current-page="params.page" :limit="params.limit" @perpage="perpage" @docurrentpage="docurrentpage" />
  </div>
</template>
<script>
import CutPage from '@/components/Cutpage'
import { allcate } from '@/api/cate'
import { getarticle, delarticle } from '@/api/article'
import { exportTableAsXLSX } from '@/utils/unitools'
export default {
  components: {
    CutPage
  },
  data() {
    return {
      checkList: ['ID', '标题', '分类名', '图片', '创建时间', '是否推荐', '是否置顶', '文章状态', '操作'],
      renderList: [{ name: 'ID', prop: 'cateid' }, { name: '标题', prop: 'title' }, { name: '分类名', prop: 'catename' }, { name: '图片', prop: 'pic' }, { name: '创建时间', prop: 'create_date' }, { name: '是否推荐', prop: 'ishot' }, { name: '是否置顶', prop: 'istop' }, { name: '文章状态', prop: 'status' }],
      catelist: [],
      articlelist: [],
      params: {
        create_date: [],
        start_time: '',
        end_time: '',
        title: '',
        cateid: '',
        catename: '',
        page: 1,
        limit: 10
      },
      flag: true,
      total: 0,
      up: 'el-icon-arrow-up',
      // 分页显隐
      isTableAlive: true
    }
  },
  async created() {
    const res = await allcate()
    console.log(res)
    this.catelist = res
    this.getArticlelist()
  },
  methods: {
    perpage(val, id) {
      console.log(val, id)
      this.params.page = 1
      this.params.limit = val
      this.getArticlelist()
    },
    docurrentpage(val) {
      this.params.page = val
      this.getArticlelist()
    },
    dodelete(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delarticle(id)
        console.log(res)
        if (res === '') {
          this.$message.success('删除成功')
          this.getArticlelist()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    exportExcel() {
      exportTableAsXLSX('outTable')
    },

    // handleSizeChange(val) {
    //   // console.log(`每页 ${val} 条`)
    //   this.params.page = 1
    //   this.params.limit = val
    //   this.getArticlelist()
    // },
    // handleCurrentChange(val) {
    //   this.params.page = val
    //   this.getArticlelist()
    // },
    async getArticlelist() {
      const res = await getarticle(this.params)
      console.log(res)
      this.articlelist = res.data
      this.total = res.count
    },

    hide() {
      this.flag = !this.flag
      this.up = this.flag ? 'el-icon-arrow-up' : 'el-icon-arrow-down'
    },
    reload() {
      this.articlelist = []
      this.params.start_time = ''
      this.params.end_time = ''
      this.params.page = 1
      this.isTableAlive = false
      setTimeout(() => {
        this.isTableAlive = true
      }, 100)
      this.getArticlelist()
    },
    reset() {
      this.params.title = ''
      this.params.create_date = ''
      this.params.cateid = ''
      this.reload()
    },
    serach() {
      console.log(this.create_date)
      this.params.start_time = this.params.create_date[0]
      this.params.end_time = this.params.create_date[1]
      this.getArticlelist()
    }

  }
}
</script>
<style lang="scss" scoped>
.page-box {
  padding: 20px;
  .search-box,
  .tabbar-box {
    padding: 10px 10px;
    height: 50px;
    .filter {
      margin-left: 10px;
    }
  }
  .fontsome {
    margin-left: 15px;
    &:hover {
      border-bottom: 1px solid #51a7ff;
      padding-bottom: 5px;
      color: #51a7ff;
    }
  }
}
.date {
  width: 250px;
}
.status {
  border: 1px solid #d9ecff;
  color: #409eff;
  background-color: #ecf5ff;
  text-align: center;
  border-radius: 5px;
  width: 60px;
  height: 30px;
  font-size: 12px;
  line-height: 30px;
}
.el-pagination {
  text-align: right;
}
</style>
